package io.jboot.admin.utils;

import com.jeesuite.filesystem.FSProvider;
import com.jeesuite.filesystem.UploadObject;
import com.jeesuite.filesystem.provider.qiniu.QiniuProvider;
import com.jfinal.upload.UploadFile;
import io.jboot.Jboot;
import io.jboot.admin.config.QiniuConfig;
import io.jboot.admin.support.log.LogInterceptor;

import java.io.File;

/**
 * Created by
 *
 * @author dw
 * @date 2018/02/22
 */
public class QiniuUtils {
    private static QiniuConfig qiniuConfig;
    private final static com.jfinal.log.Log logger = com.jfinal.log.Log.getLog(LogInterceptor.class);

    static {
        qiniuConfig = Jboot.config(QiniuConfig.class);
    }

    public static String upload(UploadFile uploadFile) {
        //七牛分布式文件系统
        FSProvider provider = new QiniuProvider("http://ovjjqjpmp.bkt.clouddn.com",
                qiniuConfig.getQiniubucketName(), qiniuConfig.getQiniuaccessKey(), qiniuConfig.getQiniusecretKey(),
                true);
        String url = provider.upload(new UploadObject(uploadFile.getFile()));
        if (null == qiniuConfig.getTmp() || qiniuConfig.getTmp().equals("")) {

        } else {
            //provider.downloadAndSaveAs(url, qiniuConfig.getTmp());
        }
        String downloadUrl = provider.getDownloadUrl(url);
        logger.debug("文件上传到七牛云私有空间成功:" + downloadUrl);
        return downloadUrl;
    }
}
